/**  
* @Title:  UserSessionFilter.java
* @Package com.colldown.iservice.login.filter
* @Description: TODO(用一句话描述该文件做什么)
* @author Wayne
* @date  2015-5-26 下午8:48:51
* @version V1.0  
* Update Logs:
* ****************************************************
* Name:
* Date:
* Description:
******************************************************
*/
package com.colldown.iservice.login.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

/**
 * @ClassName: UserSessionFilter
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Wayne
 * @date 2015-5-26 下午8:48:51
 *
 */
/** 
 * 登录过滤 
 *  
 */  
public class UserSessionFilter extends OncePerRequestFilter {  
    @Override  
    protected void doFilterInternal(HttpServletRequest request,  
            HttpServletResponse response, FilterChain filterChain)  
            throws ServletException, IOException {  
        // 不拦截的url  
        String[] notFilter = new String[] {"/login.jsp","loginForm","/css","/dataTables","/Highcharts","/images","/js"};  
  
        // 请求的url  
        String url = request.getRequestURI();  
          
        if(url.indexOf("iService") != -1){  
            boolean doFilter = chek(notFilter,url);  
            if(doFilter){  
                Object obj = request.getSession().getAttribute("userSession");  
                if(null==obj){  
                    request.setCharacterEncoding("UTF-8");  
                    response.setCharacterEncoding("UTF-8"); 
                    // 如果session中不存在登录者实体，则弹出框提示重新登录  
                    PrintWriter out = response.getWriter();  
                    String loginPage = request.getContextPath()+"/login.jsp";  
                    StringBuilder builder = new StringBuilder();  
                    builder.append("<script type=\"text/javascript\">");  
                    builder.append("window.top.location.href='");  
                    builder.append(loginPage);  
                    builder.append("';");  
                    builder.append("</script>");  
                    out.print(builder.toString());  
                }else {  
                    filterChain.doFilter(request, response);  
                }  
            }else{  
                filterChain.doFilter(request, response);  
            }  
        }else{  
            filterChain.doFilter(request, response);  
        }  
    }  
      
    /** 
     * @param notFilter 不拦截的url 
     * @param url ：请求的url 
     * @return false：不拦截 
     *         true：拦截 
     */  
    public boolean chek(String[] notFilter,String url){  
        //url以css和js结尾的不进行拦截  
        if(url.endsWith(".css") || url.endsWith(".js")){  
            return false;  
        } 
        //含有notFilter中的任何一个则不进行拦截  
        for (String s : notFilter) {  
            if (url.indexOf(s) != -1) {  
                return false;  
            }  
        }  
        return true;  
    }  
}